package com.ggy.babybed.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;

import org.xclcharts.chart.DialChart;
import org.xclcharts.common.MathHelper;
import org.xclcharts.renderer.XEnum;
import org.xclcharts.renderer.plot.PlotAttrInfo;
import org.xclcharts.view.GraphicalView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ggy on 2016/5/30.
 */
public class MainView extends GraphicalView {
    private final String TAG = "MainView";
    private DialChart chart = new DialChart();
    private float mPercentage = 0.6f;

    public MainView(Context context) {
        super(context);
        chartRender();
    }

    public MainView(Context context, AttributeSet attrs) {
        super(context, attrs);
        chartRender();
    }

    public MainView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        chartRender();
    }

    @Override
    public void render(Canvas canvas) {
        try {
            chart.render(canvas);
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        chart.setChartRange(w, h);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        Log.v(TAG,"is on the layout!");
        super.onLayout(changed, left, top, right, bottom);
        chartRender();
        refreshChart();
    }

    public void chartRender() {
        try {
//          chart.setApplyBackgroundColor(true);
//          chart.setBackgroundColor(R.color.content_bg);
            //设置指针长度
            chart.getPointer().setLength(0.68f);

            addAxis();
            addAttrInfo();

            chart.getPointer().setPercentage(mPercentage);

            chart.getPointer().getPointerPaint().setColor(Color.WHITE);
            chart.getPointer().getBaseCirclePaint().setColor(Color.WHITE);
            chart.getPointer().setPointerStyle(XEnum.PointerStyle.TRIANGLE);

        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    private void addAxis() {
        //开始设置轴
        //轴1 --最外面的弧线轴
        chart.addArcLineAxis(1.0f);

        //轴3 --环形颜色轴
        List<Float> ringPercentage = new ArrayList<>();
        float rper = MathHelper.getInstance().sub(1, mPercentage);
        ringPercentage.add(mPercentage);
        ringPercentage.add(rper);

        List<Integer> rcolor = new ArrayList<>();
        rcolor.add(Color.rgb(222, 248, 239));
        rcolor.add(Color.rgb(99, 214, 173));
        chart.addStrokeRingAxis(0.8f, 0.7f, ringPercentage, rcolor);

        chart.addLineAxis(XEnum.Location.TOP, 0.3f);
        chart.addLineAxis(XEnum.Location.LEFT, 0.3f);
        chart.addLineAxis(XEnum.Location.RIGHT, 0.3f);
        if (chart.getPlotAxis().size() >= 2) {
            chart.getPlotAxis().get(2).getAxisPaint().setColor(Color.BLUE);
            chart.getPlotAxis().get(2).getAxisPaint().setStrokeWidth(5);
        }
        if (chart.getPlotAxis().size() >= 3) {
            chart.getPlotAxis().get(3).getAxisPaint().setColor(Color.GREEN);
            chart.getPlotAxis().get(3).getAxisPaint().setStrokeWidth(5);
        }
        if (chart.getPlotAxis().size() >= 4) {
            chart.getPlotAxis().get(4).getAxisPaint().setColor(Color.YELLOW);
            chart.getPlotAxis().get(4).getAxisPaint().setStrokeWidth(5);
        }

        chart.getPlotAxis().get(0).getAxisPaint().setColor(Color.WHITE);
        chart.getPlotAxis().get(0).getAxisPaint().setStrokeWidth(2);
        chart.getPlotAxis().get(1).getFillAxisPaint().setColor(Color.rgb(47, 199, 140));

        chart.addCircleAxis(0.2f, Color.rgb(62, 175, 135));
        chart.addCircleAxis(0.15f, Color.rgb(28, 111, 84));
    }

    private void addAttrInfo() {

        PlotAttrInfo plotAttrInfo = chart.getPlotAttrInfo();

        //设置附加信息
        Paint paintTB = new Paint();
        paintTB.setColor(Color.WHITE);
        paintTB.setTextAlign(Paint.Align.CENTER);
        paintTB.setTextSize(30);
        plotAttrInfo.addAttributeInfo(XEnum.Location.TOP, "50%", 0.9f, paintTB);
        plotAttrInfo.addAttributeInfo(XEnum.Location.LEFT, "20%", 0.9f, paintTB);
        plotAttrInfo.addAttributeInfo(XEnum.Location.RIGHT, "80%", 0.9f, paintTB);

        Paint paintBT = new Paint();
        paintBT.setColor(Color.WHITE);
        paintBT.setTextAlign(Paint.Align.CENTER);
        paintBT.setTextSize(27);
        plotAttrInfo.addAttributeInfo(XEnum.Location.BOTTOM, "温度：26℃    湿度：60%RH    紫外线：56%    PM2.5:56", 1.0f, paintBT);

        Paint paintCT = new Paint();
        paintCT.setColor(Color.WHITE);
        paintCT.setTextAlign(Paint.Align.CENTER);
        paintCT.setTextSize(42);
        plotAttrInfo.addAttributeInfo(XEnum.Location.BOTTOM, "良好", 0.5f, paintCT);
    }

    public void setCurrentStatus(float percentage) {
        //清理
        chart.clearAll();
        mPercentage = percentage;
        //设置当前百分比
        chart.getPointer().setPercentage(mPercentage);
        addAxis();
        addAttrInfo();
    }

}
